package com.dcm.groupbuy.service.impl;


import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.common.service.RedisService;
import com.dcm.common.utils.IdWorker;
import com.dcm.groupbuy.bo.NumberBo;
import com.dcm.groupbuy.bo.OrderBo;
import com.dcm.groupbuy.entity.GroupOrderEntity;
import com.dcm.groupbuy.mapper.GroupOrderMapper;
import com.dcm.groupbuy.mapper.OrderBoMapper;
import com.dcm.groupbuy.service.GroupOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Value;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 牛浩
 */
@Service
public class GroupOrderServiceImpl implements GroupOrderService {
    @Autowired
    private GroupOrderMapper groupOrderMapper;
    @Autowired
    private OrderBoMapper orderBoMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public List<GroupOrderEntity> findAll() {
        List<GroupOrderEntity> all = groupOrderMapper.findAll();
        logger.info(all.toString());
        return all;
    }

    @Override
    public List<GroupOrderEntity> search(Integer groupState) {
        if (groupState == null){
            throw new MyException(Constants.GroupOrderErrorMsg.STATE_CANNOT_NULL);
        }
        return groupOrderMapper.search(groupState);

    }

    @Autowired
    private RedisService redisService;
    @Override
    public void insertGroupOrder(GroupOrderEntity groupOrderEntity) throws MyException{
        /**
         * 查询有没有空值 有的话抛出异常
         */
        checkGroupOrder(groupOrderEntity);
        Date date = new Date();
        groupOrderEntity.setStartDate(date);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        /**
         * 把日期往后增加一天,整数  往后推,负数往前移动
         */
        calendar.add(Calendar.DATE,1);
        /**
         * 这个时间就是日期往后推一天的结果
         */
        date=calendar.getTime();
        groupOrderEntity.setEndDate(date);
        groupOrderEntity.setJoinPeople(1);
        Long id = new IdWorker().nextId();
        groupOrderEntity.setGroupId(id);
        /**
         * 开启拼团的人
         */
        groupOrderMapper.insertGroupOrder(groupOrderEntity);
        /**
         * 往redis存
         */
        ArrayList<GroupOrderEntity> groupOrderEntities = new ArrayList<>();
        List<GroupOrderEntity> groupOrderEntityList = (List<GroupOrderEntity>)redisService.get("开团");
        if (groupOrderEntityList!=null){
            for (GroupOrderEntity orderEntity : groupOrderEntityList) {
                groupOrderEntities.add(orderEntity);
            }
        }
        groupOrderEntities.add(groupOrderEntity);
        redisService.set("开团",groupOrderEntities,86400);
        /**
         * 往拼团订单添加 第一个人
         */
        OrderBo orderBo = new OrderBo();
        orderBo.setId(id+1);
        orderBo.setOrderSn(id+1+"");
        /**
         * 发起人姓名
         */
        orderBo.setMemberUsername(groupOrderEntity.getTeamLeader());
        /**
         * 拼团人姓名
         */
        orderBo.setReceiverName(groupOrderEntity.getTeamLeader());
        /**
         * 拼团需要人数
         */
        orderBo.setPeoples(groupOrderEntity.getGroupPeople());
        /**
         * 拼团商品
         */
        orderBo.setNote(groupOrderEntity.getGroupName());
        /**
         * 添加redis团员
         */
        ArrayList<OrderBo> orderBoArrayList = new ArrayList<>();
        List<OrderBo> orderEntityList = (List<OrderBo>)redisService.get("参团");
        if (orderEntityList!=null){
            for (OrderBo bo : orderEntityList) {
                orderBoArrayList.add(bo);
            }
        }
        orderBoArrayList.add(orderBo);
        redisService.set("参团",orderBoArrayList,86400);
        orderBoMapper.insertOrderBo(orderBo);
    }

    @Override
    public NumberBo searchNum() {
        return orderBoMapper.searchNum();
    }

    @Override
    public List<GroupOrderEntity> dateSearch(Integer state) {
        return groupOrderMapper.dateSearch(state);
    }

    @Override
    public PageInfo<GroupOrderEntity> findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<GroupOrderEntity> list = groupOrderMapper.findAll();
        PageInfo<GroupOrderEntity> page = new PageInfo<>(list);
        return page;
    }

    private static void checkGroupOrder(GroupOrderEntity groupOrderEntity)  {
        if (groupOrderEntity.getGroupState()==null){
            throw new MyException(Constants.GroupOrderErrorMsg.STATE_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(groupOrderEntity.getGroupName())){
            throw new MyException(Constants.GroupOrderErrorMsg.NAME_CANNOT_NULL);
        }
        if (groupOrderEntity.getGroupPeople()==null){
            throw new MyException(Constants.GroupOrderErrorMsg.PEOPLE_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(groupOrderEntity.getTeamLeader())){
            throw new MyException(Constants.GroupOrderErrorMsg.LEADER_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(groupOrderEntity.getImg())){
            throw new MyException(Constants.GroupOrderErrorMsg.IMG_CANNOT_NULL);
        }
    }
}
